package cn.ai.shoes.core.handler;


import cn.ai.shoes.common.pojo.entity.CommonResult;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import java.io.IOException;

import static cn.ai.shoes.common.exception.constant.GlobalErrorCodeConstants.USER_PASSWORD_ERROR;


public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    /**
     * 登录失败
     * @param request
     * @param response
     * @param exception
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        //将result象转换为json字符串
        String json= JSON.toJSONString(CommonResult.error(USER_PASSWORD_ERROR));
        //设置响应头
        response.setContentType("application/json;charset=utf-8");
        //设置响应体
        response.getWriter().print(json);
    }
}
